package uc.org.jkas.app.admin;
import java.util.HashMap;
import uc.org.jkas.model.AdminsM;
import uc.org.jkas.model.RolesM;
public class Login extends LoginBase
{  AdminsM amsm=new AdminsM();
   RolesM rsm=new RolesM();
	public void index()
    {  if(IS_POST())
		{String res="{\"code\":400,\"msg\":\"failed\"}";
		 String username=P("username");
		 String password=P("password");
		 String capacha=Plow("capacha");
	   if(capacha.equals(sess2str("verCode")))
	    {
		  if(!isEmpty(username) && !isEmpty(password))
			{
			  HashMap<String,String> user=amsm.getByName(username,true);
			  if(user!=null && !user.isEmpty())
				{String pass=md5(strc(md5(password),user.get("salt")));
				  if(pass.equals(user.get("password")))
					{
					 HashMap<String,Object> data=new HashMap<String,Object>();
					 data.put("lastlogintime",time());
					 data.put("lastloginip",ip2int(clientIp()));
					 amsm.update(strc("uid=",user.get("uid")),data);
					 int uid=toInt(user.get("uid"));
					 if(uid==1)
						{user.put("privis","*");
					    }
						 else
						{ 
					      String role_id=user.get("role_id");
						  String privis=rsm.getPrivis(role_id);
						  user.put("privis",privis);
                        }
                     
					 session("user",user);
			         res="{\"code\":200,\"msg\":\"ok\"}";
					}
					else
					{res="{\"code\":201,\"msg\":\"wrong password\"}";
					}
				}
				else
				{res="{\"code\":202,\"msg\":\"no user\"}";
				}

			  
			  
			}
			else
			{res="{\"code\":203,\"msg\":\"empty username or password\"}";
			}
		}
		else
			{res="{\"code\":204,\"msg\":\"wrong capacha\"}";
			}
          echo(res);
		}
		else
		{
		 display();
		}
	}

	public void findpwd()
	{if(IS_POST())
		{
		}
		else
		{display();
		}
	}

	public void resetpwd()
	{if(IS_POST())
	 {String res="{\"code\":400,\"msg\":\"failed\"}";
		 String username=P("username");
		 String email=P("email");
		 String capacha=Plow("capacha");
	   if(capacha.equals(sess2str("verCode")))
	    {
		  if(!isEmpty(username) && !isEmpty(email))
			{HashMap<String,String> user=amsm.getByName(username,true);
			  if(!user.isEmpty())
				{if(email.equals(user.get("email")))
					{ HashMap<String,Object>params=new HashMap<String,Object>();
				      int salt=random(100000,999999);
					  String newpass=randStr();
				      String password=md5(strc(md5(newpass),salt));
				      params.put("password",password);
				      params.put("salt",salt);
			          if(amsm.update(strc("uid=",user.get("uid")),params)>0)
				       { String result=mail(email,"管理员重置密码",strc("您的登录密码已重置为：",newpass,",请登录后重新修改登录密码！"));
				          if("1".equals(result)){res=JSON_RESULT_SUCCESS;}
				       }
					}
					else
					{res="{\"code\":201,\"msg\":\"wrong email\"}";
					}
				}
				else
				{res="{\"code\":202,\"msg\":\"no user\"}";
				}
			}
			else
			{res="{\"code\":203,\"msg\":\"empty username or email\"}";
			}
		}
		else
			{res="{\"code\":204,\"msg\":\"wrong capacha\"}";
			}
       echo(res);
     }
     else
	 {display();
	 }
	}

	public void loginout()
	{sessdel("user");
	 redirect(url("/admin/login"));
	}
}